﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Agents.Admin
{
    //This page allows the administrator to choose an agent from the data grid for view/erase purposes.
    public partial class ChooseEditAgent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        //function redirects the administrator to the chosen agents view page. 
        protected void DataGridProjects_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            // Check which command
            switch (e.CommandName)
            {
                case ("View"):
                {
                    //retrieving the id of the chosen agent.
                    int agentId = Convert.ToInt32(e.Item.Cells[0].Text);

                    Response.Redirect("ShowAgent.aspx?agentId=" + agentId);
                    break;
                }
                case ("Delete"):
                {
                    //retrieving the id of the chosen agent.
                    int agentId = Convert.ToInt32(e.Item.Cells[0].Text);

                    Queries queries = new Queries();
                    queries.DeleteAgent(agentId);

                    // Refresh the grid
                    DataGridProjects.DataBind();
                    break;
                }
                case ("Edit"):
                {
                    //retrieving the id of the chosen agent.
                    int agentId = Convert.ToInt32(e.Item.Cells[0].Text);

                    Response.Redirect("~/Admin/EditAgent.aspx?agentId=" + agentId);
                    break;
                }
                default:
                {
                    break;
                }
            }
        }

        //function handles the page indexing.
        protected void DataGridProjects_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            //saving the index chosen by the user.
            int index = e.NewPageIndex;
            //updating the current page for the data grid to display.
            DataGridProjects.CurrentPageIndex = index;
            //binding the data.
            DataGridProjects.DataBind();
        }

        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/AdminMenu.aspx");
        }

        /// <summary>
        /// Occurs when the user filters or unfilters the results by
        /// the agent's name, and updates results.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            // Filter
            if ((btnFilter.Text == "סנן") && (!String.IsNullOrEmpty(txtAgentName.Text)))
            {
                SqlDataSource1.FilterExpression = String.Format("Name LIKE '%{0}%'", txtAgentName.Text);
                btnFilter.Text = "בטל סינון";
            }
            // Unfilter
            else
            {
                SqlDataSource1.FilterExpression = null;
                btnFilter.Text = "סנן";
            }
        }
    }
}